
/**
 * void movePlayer 
 */
void movePlayer(PlayerObject *player) {
	
	float friction = 0.2f;
	float limitVel = 0.0005f;
	// x
	if (player->velocity.x < 0.0f) {
		player->velocity.x += ((friction / player->mass) * (6.0f / fps));							
		
	}
	if (player->velocity.x > 0.0f) {
		player->velocity.x -= ((friction / player->mass) * (6.0f / fps));			
	}
	
	if (fabsf(player->velocity.x) < limitVel && player->velocity.x != 0.0f) {				
		player->velocity.x = 0.0f;
	}
	
	// y
	if (player->velocity.y < 0.0f) {
		player->velocity.y += ((friction / player->mass) * (6.0f / fps));							
		
	}
	if (player->velocity.y > 0.0f) {
		player->velocity.y -= ((friction / player->mass) * (6.0f / fps));			
	}
	
	if (fabsf(player->velocity.y) < limitVel && player->velocity.y != 0.0f) {				
		player->velocity.y = 0.0f;
	}
	
	// directionalVelocity
	if (player->directionalVelocity < 0.0f) {
		player->directionalVelocity += ((friction*2 / player->mass) * (6.0f / fps));							
		
	}
	if (player->directionalVelocity > 0.0f) {
		player->directionalVelocity -= ((friction / player->mass) * (6.0f / fps));			
	}
	
	if (fabsf(player->directionalVelocity) < limitVel && player->directionalVelocity != 0.0f) {				
		player->directionalVelocity = 0.0f;
	}	
	
	
	// XYZ velocity, alla externa krafter som påverkar
	// directionalVelocity
	
	float time = glfwGetTime();	
	player->angle.z = player->angle.z + 0.001*sin(time/2);

//	player->angle.x = cos(player->angle.z-M_PI/2);	
//	player->angle.y = sin(player->angle.z);	
	
	player->pos.x += player->velocity.x + cos(player->angle.z) * player->directionalVelocity;
	player->pos.y += player->velocity.y + sin(player->angle.z) * player->directionalVelocity;			
	player->pos.z = fabsf(0.002*sin(time/2));
}



/**
 * void controlPlayer only moves the player if no effects are active
 */
//void controlPlayer(PlayerObject *player, char up, char down, char left, char right, char special) {
void controlPlayer(PlayerObject *player, int up, int down, int left, int right, int special) {
	if (player->effect == 0 && player->isDead == 0) {
		
		// Set gamespeed accourding to FPS
		player->angleRotation.z = (player->directionalVelocity + 0.3f) * 6.0f / fps;	
		if (player->angleRotation.z > 0.1f) {
			player->angleRotation.z = 0.1f;
		}

		// LEFT, RIGHT
		if(glfwGetKey(left)) {
			player->angle.z += player->angleRotation.z;			

			//camera rotation1
			player->cameraAngle -= player->angleRotation.z;		
		}
		if(glfwGetKey(right)) {
			player->angle.z -= player->angleRotation.z;
			
			//camera rotation
			player->cameraAngle += player->angleRotation.z;	
		}
		
		// FORWARD KEY
		if (glfwGetKey(up)){
			if (player->directionalVelocity < player->maxVelocity) {
				player->directionalVelocity += ((player->force / player->mass) * (6.0f / fps));
			}				
		} 
		
		// BACKWARD KEY
		if (glfwGetKey(down)) {
			if (player->directionalVelocity > -0.2f) {
				player->directionalVelocity -= ((player->force / player->mass) * (6.0f / fps));			
			}						
		} 
		
		if (glfwGetKey(special)) {
			float timeSinceLast = glfwGetTime() - player->lastShootTime;
			if (player->weapon == WEAPON_CANNON && timeSinceLast > 1.0 && player->ammo > 0) {
				XYZ angle;
				angle.x = 0.0f;
				angle.y = 0.0f;
				angle.z = 0.0f;
				XYZ velocity;
				velocity.x = cos(player->angle.z)*2.0;
				velocity.y = sin(player->angle.z)*2.0;
				velocity.z = 0.0f;		
				Location pos;
				pos.x = player->pos.x;
				pos.y = player->pos.y;
				pos.z = player->pos.z;
				pos.z += 0.5;
				addBullet(player, 9,6, 33, PLAYER_EFFECT_EXPLODE, 0, pos, velocity, player->angle);
				
				player->lastShootTime = glfwGetTime();
				player->ammo--;
			}
			
			if (player->weapon == WEAPON_MINE && timeSinceLast > 1.0 && player->ammo > 0) {
				
				XYZ angle;
				angle.x = 0.0f;
				angle.y = 0.0f;
				angle.z = 0.0f;
				XYZ velocity;
				velocity.x = 0.0f;
				velocity.y = 0.0f;
				velocity.z = 0.0f;
				Location pos = player->pos;
				pos.z += 1.5;
				printf("Adding mine (x,y,z)=%f\n",pos.z);
				addBullet(player, 4, 2, 50, PLAYER_EFFECT_SINK, bullet_bouy, pos, velocity, angle);
				
				player->lastShootTime = glfwGetTime();
				player->ammo--;
			}
			
			if (player->weapon == WEAPON_SWIRLBOMB && timeSinceLast > 1.0 && player->ammo > 0) {
				
				XYZ angle;
				angle.x = 0.0f;
				angle.y = 0.0f;
				angle.z = 0.0f;
				XYZ velocity;
				velocity.x = 0.0f;
				velocity.y = 0.0f;
				velocity.z = 0.0f;
				Location pos = player->pos;
				pos.z += 1.5;
				printf("Adding mine (x,y,z)=%f\n",pos.z);
				addBullet(player, 7, 7, 150, PLAYER_EFFECT_WHIRL_POOL, bullet_bouy, pos, velocity, angle);
				
				player->lastShootTime = glfwGetTime();
				player->ammo--;
			}
		}
	}

	
	//Camera movement, separate ??
	if( player->cameraAngle > 0.0f)
    {
        player->cameraAngle -= player->angleRotation.z * fabsf(2.0*sinf(player->cameraAngle));
    }
    if( player->cameraAngle <  0.0f)
    {
        player->cameraAngle += player->angleRotation.z * fabsf(2.0*sinf(player->cameraAngle));
    }

	if (player->effect == 0 || player->effect == PLAYER_EFFECT_EXPLODE) {
		movePlayer(player);
	}
}


